﻿namespace ZXW.DotNet.Common.Core
{
    #region Using

    using System.Text.RegularExpressions;

    #endregion

    /// <summary>
    /// 数据校验类
    /// </summary>
    public class Validate
    {
        #region 字段
        private static readonly Regex RegNumber = new Regex("^[0-9]+$");
        private static readonly Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static readonly Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static readonly Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");
        private static readonly Regex RegEmail = new Regex(@"^(.+)@(.+)$");
        private static readonly Regex RegChzn = new Regex("[\u4e00-\u9fa5]");
        private static readonly Regex RegSmallDate = new Regex(@"^(\d{4})-(\d{1,2})-(\d{1,2})$");
        private static readonly Regex RegIpAddress = new Regex(@"^(\d+)\.(\d+)\.(\d+)\.(\d+)");
        #endregion

        #region 数字字符串检查

        /// <summary>
        /// 是否数字字符串
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string inputData)
        {
            Match m = RegNumber.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否数字字符串可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(string inputData)
        {
            Match m = RegNumberSign.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮点数
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(string inputData)
        {
            Match m = RegDecimal.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮点数可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(string inputData)
        {
            Match m = RegDecimalSign.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 中文检测
        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsHasChzn(string inputData)
        {
            Match m = RegChzn.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 邮件地址
        /// <summary>
        /// 是否是浮点数可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 验证日期格式(yyyy-MM-dd)
        /// <summary>
        /// 验证日期格式(yyyy-MM-dd)
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsSmallDate(string inputData)
        {
            Match m = RegSmallDate.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 验证IP地址
        /// <summary>
        /// 验证IP地址
        /// </summary>
        /// <param name="inputData">IP地址</param>
        /// <returns></returns>
        public static bool IsIpAddress(string inputData)
        {
            Match m = RegIpAddress.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 其他
        /// <summary>
        /// 检查字符串最大长度，返回指定长度的串
        /// </summary>
        /// <param name="sqlInput">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns></returns>
        public static string SqlText(string sqlInput, int maxLength)
        {
            if (!string.IsNullOrEmpty(sqlInput))
            {
                sqlInput = sqlInput.Trim();
                if (sqlInput.Length > maxLength)//按最大长度截取字符串
                    sqlInput = sqlInput.Substring(0, maxLength);
            }
            return sqlInput;
        }
        #endregion
    }
}
